﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebeTerm.ClearQ;
using WebeTerm.RobotBook;
using System.IO;
using System.Web.Script.Serialization;
using WebeTerm.Common;
using System.Net;
using WebeTerm.GetRebate;
using System.Text.RegularExpressions;

namespace WebeTerm.PlugInsTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();

            RebateConfig rbtConfig = new RebateConfig(){
                 AirWayCode =@"HU",
                 Code = string.Empty,
                 CodeUrl = @"http://agent.hnair.com/hub2b/servlet/com.travelsky.web.huet.user.GenImage",
                 Password = @"bj578090",
                 PasswordControl = @"password",
                 PnrNo = string.Empty,
                 RequestUrl = @"http://agent.hnair.com/hub2b/b2b_pnr_db.do?actionType=getInfoDisplay&isHaveNoSeg=0&pnr=",
                 UserName = @"sjw161",
                 UserNameControl = @"userid",
                 VerifyCodeControl = @"checkcode",
                 WebLogInUrl = @"http://agent.hnair.com/hub2b/login.do"
            };

            HURebatePlugIn rebate = new HURebatePlugIn();

            rebate.PlugInConfigJSon = jss.Serialize(rbtConfig);

            rebate.GetVeryCode();
            rebate.LogIn("");


            //rebate.PlugInConfigJSon
            //HttpHeader header = new HttpHeader();
            //header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
            //header.contentType = "application/x-www-form-urlencoded";
            //header.method = "POST";
            //header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            //header.maxTry = 300;
            //string vCode = string.Empty;
            //string Code = string.Empty;
            //CookieContainer cookie=  HttpHelper.GetCooKie(@"http://agent.hnair.com/", string.Empty, header);
            //HttpHelper.DowloadCheckImg(@"http://agent.hnair.com/hub2b/servlet/com.travelsky.web.huet.user.GenImage", cookie, @"d:\HU.JPG");

            //string result = HttpHelper.POST(@"http://agent.hnair.com/hub2b/login.do", @"userid=sjw161&password=bj578090&checkcode=" + vCode, cookie, header);


            //result = HttpHelper.POST(@"http://agent.hnair.com/hub2b/b2b_pnr_db.do?actionType=getInfoDisplay&isHaveNoSeg=0&pnr="+Code, string.Empty, cookie, header);

            //PJ3W3F
            RobotBookConfig plugInConfig = new RobotBookConfig();
            using (FileStream fs = new FileStream(@"D:\SouceCode\Personal\Custom Source\Cabins.txt", FileMode.Open))
            {
                StreamReader sr = new StreamReader(fs,Encoding.UTF8);
                sr.ReadToEnd().Split(new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(l => {
                    var singleRow  = l.Split(new char[] { ':'}, StringSplitOptions.RemoveEmptyEntries).ToList();
                    int indexOf = 100;
                    singleRow[1].Split(new char[] {' ','\t' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(c => {
                        CabinChar item = new CabinChar() { AirWay = singleRow[0] };
                        item.CabinCharCode = c;
                        item.Weight = indexOf--;
                        plugInConfig.CabinChars.Add(item);
                    });
                });
            }

            /*
            plugInConfig.Requires.Add(new RequireBook()
            {
                AirWay = @"HU",
                Dept = @"SHA",
                DeptDate = DateTime.Now.AddDays(30).ToString(@"yyyy-MM-dd"),
                Dest = @"CAN",
                RetDate = DateTime.Now.AddDays(50).ToString(@"yyyy-MM-dd"),
                Passengers = new List<Passenger>() { new Passenger() { IdNo = @"430422198209077938", Mobile = @"13524008692", Name = @"胡李俊" } },
            });
            */

            plugInConfig.Requires.Add(new RequireBook()
            {
                AirWay = @"3U",
                Dept = @"WUH",
                DeptDate = DateTime.Now.AddDays(40).ToString(@"yyyy-MM-dd"),
                Dest = @"NTG",
                Passengers = new List<Passenger>() { new Passenger() { IdNo = @"430422198209077938", Mobile = @"13524008692", Name = @"胡李俊" } },
            });



            RobotBookPlugIn plugIn = new RobotBookPlugIn();

            plugIn.PlugInConfigJSon = jss.Serialize(plugInConfig);

            plugIn.Config = new PlugIns.eTermConfig() {
                eTermAddress = @"bj565677729.xicp.net",
                eTermPort=350,
                eTermSessionType= eTerm.NetSocket.SocketConnectType.User350,
                Password = @"123000",
                siText=@"SI",
                userName = @"hu1"
            };
            plugIn.OneTermDisconnected += (o, e) =>
            {
                Console.WriteLine(@"OneTermDisconnected...");
            };
            plugIn.OneTermValidated += (o, e) =>
            {
                Console.WriteLine(@"OneTermValidated...");
            };
            plugIn.OneTermReadPacket += (o, e) =>
            {
                Console.WriteLine(string.Format(@"----------------------------
{0}
{1}
----------------------------", e.PacketName, e.PacketResult));
            };
            plugIn.OnUnPackResult += (o, e) =>
            {
                //if(Regex.IsMatch(e.PlugInResult.ToDisplay(),@"返扣\:", RegexOptions.IgnoreCase| RegexOptions.Multiline))
                //    Console.WriteLine(e.PlugInResult.ToDisplay());
            };

            plugIn.UnPacketResultWorks.Add(new PlugIns.UnPacketStreamWorker((e)=>{
                Console.WriteLine(e.PlugInResult.ToDisplay());
                var value = e.PlugInResult.ToDisplay();
                var m = Regex.Matches(value, @"((PNR|大编号)\:([A-Z0-9]+))+", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (m.Count==2) {
                    rbtConfig.PnrNo = m[1].Groups[3].Value;
                    rbtConfig.Code = m[0].Groups[3].Value;
                    rebate.PlugInConfigJSon = jss.Serialize(rbtConfig);

                    rebate.UnPacketResultWorks.Add(new PlugIns.UnPacketStreamWorker((e1) => {
                        Console.WriteLine(e1.PlugInResult.ToDisplay());
                    }));

                    //rebate.Execute();
                    //rbtConfig.PnrNo = m.Groups[2].Value;
                    //rbtConfig.Code = m.Groups[2].Value;
                }
                //if(e.PlugInResult.
            }));

            plugIn.Execute();

            //plugIn.Connect(eTerm.NetSocket.SocketConnectType.User350, @"bj565677729.xicp.net", 350, @"liguowei11", @"1");
            //plugIn.Connect(eTerm.NetSocket.SocketConnectType.User443, @"pek3.eterm.com.cn", 443, @"o73352e1", @"shzs141022", @"si:44086/168800a", 0x2E, 0x51);
            //plugIn.Connect(eTerm.NetSocket.SocketConnectType.User350, @"222.73.205.23", 350, @"shdyadmin", @"dongyao@3212");
            Console.ReadLine();
            
        }
    }
}
